package com.pig4cloud.pigx.user.controller.feign;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import com.pig4cloud.pigx.user.entity.User;
import com.pig4cloud.pigx.user.mapstruct.UserMobileStruct;
import com.pig4cloud.pigx.user.service.UserService;
import com.pig4cloud.pigx.user.vo.UserMobileVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description: 平台用户Feign接口
 * @Date: 2022/10/15 12:56
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/userFeign")
@Api(value = "user", tags = "平台用户管理")
public class UserFeignImpl {

    /**
     * 平台用户
     */
    private final UserService userService;

    /**
     * 获取用户信息，token登录验证时调用
     *
     * @param username
     * @return
     */
    @Inner
    @ApiOperation(value = "获取用户信息", notes = "获取用户信息 token登录验证时调用")
    @GetMapping("/userInfo/{username}")
    public R userInfo(@PathVariable("username") String username) {
        User user = userService.getOne(Wrappers.<User>lambdaQuery()
                .eq(User::getAddress, username));
        return R.ok(user);
    }

    /**
     * 根据AccessKey查询用户信息
     *
     * @param accessKey 访问密钥
     * @return {@link R}
     * @since 2022/11/7 18:09
     */
    @GetMapping("/userInfo/access-key")
    public R userInfoByAccessKey(String accessKey) {
        return R.ok(userService.userInfoByAccessKey(accessKey));
    }

    @Inner
    @ApiOperation(value = "根据uid获取用户信息集合", notes = "根据uid获取用户信息集合")
    @PostMapping("/getUserList")
    public R getUserList(@RequestBody List<Long> userIdList) {
        List<User> list = userService.list(new LambdaQueryWrapper<User>()
                .in(User::getUid, userIdList)
        );
        List<UserMobileVO> userMobileVOList = UserMobileStruct.INSTANCE.userList2VoList(list);
        return R.ok(userMobileVOList);
    }

    @Inner
    @ApiOperation(value = "根据uid获取用户信息", notes = "根据uid获取用户信息")
    @GetMapping("/{userId}")
    public R getUserByUserId(@PathVariable Long userId) {
        User user = userService.getOne(new LambdaQueryWrapper<User>()
                .eq(User::getUid, userId)
        );
        UserMobileVO userMobileVO = UserMobileStruct.INSTANCE.user2Vo(user);
        return R.ok(userMobileVO);
    }


    @ApiOperation(value = "根据email获取用户信息", notes = "根据email获取用户信息")
    @GetMapping("/email")
    public R getUserByEmail(@RequestParam("email") String email) {
        List<User> user = userService.list(new LambdaQueryWrapper<User>()
                .like(User::getEmail, email)
        );
        return R.ok(user);
    }

}

